import org.apache.spark.sql.SparkSession

val spark = SparkSession.builder
val salesDF = spark.read.format("csv")
  .option("header", "true")
  .option("inferSchema", "true")
  .load("sales_data.csv")

// 加载product_info.csv并注册为临时表
val productInfoDF = spark.read.format("csv")
  .option("header", "true")
  .option("inferSchema", "true")
  .load("product_info.csv")
productInfoDF.createOrReplaceTempView("product_info")

// 把sales_data.csv也注册为临时表
salesDF.createOrReplaceTempView("sales_data")

// 进行JOIN操作并展示结果
val resultDF = spark.sql("""
  SELECT p.Product, p.Category, SUM(s.Quantity) AS TotalSales
  FROM sales_data s
  JOIN product_info p ON s.Product = p.Product
  GROUP BY p.Product, p.Category
""")
resultDF.show()

